<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>DevTest</title>
    <link rel="stylesheet" href="/3rdparty/layui/css/layui.css">
    <style>
        [v-cloak] {
            display: none;
        }

        h1 {
            line-height: 60px;
            font-weight: bold;
        }

        .m-post .m-post-footer {
            text-align: right;
            color: #777777;
            font-size: 10px;
        }
        .m-post p{
            word-break: break-word;
        }
        .error {
            color: red;
        }
        .logoutbtn{
            float: right;
            margin-top: -50px;
        }
    </style>
</head>
<body>
<div class="layui-container" id="app" v-cloak>
    <div class="layui-card">
        <div class="layui-card-body">
            <h1>DevTest 应用测试平台1111</h1>
            <button type="button" class="layui-btn layui-btn-danger logoutbtn" @click="logout">Logout</button>
        </div>
    </div>
    <div class="layui-col-xs4">
        <div class="layui-card">
            <div class="layui-card-header" style="font-size: 20px;" id="userNameDiv">{{username}}</div>
            <div class="layui-card-body">
                <div>
                    <a href="/user">
                        <button class="layui-btn">Following<span class="layui-badge layui-bg-gray" id="following">{{followData.following}}</span>
                        </button>
                    </a>
                </div>
                <br>
                <div>
                    <a href="/user">
                        <button class="layui-btn layui-btn-warm">Followers<span class="layui-badge layui-bg-gray"
                                                                                id="followers">{{followData.followers}}</span>
                        </button>
                    </a>
                </div>
            </div>
        </div>
    </div>
    <div class="layui-col-xs8">
        <div class="layui-card">
            <div class="layui-card-body">
                <div class="layui-card">
                    <div class="layui-card-body">
                        <textarea placeholder="post my life" class="layui-textarea" v-model="postContent"></textarea>
                        <br/>
                        <div class="layui-form-item">
                            <div class="layui-input-block" style="text-align: right">
                                <button @click="addPost" type="button" class="layui-btn layui-btn-normal" lay-submit=""
                                        lay-filter="demo1">Post
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
                <div id="posts">
                    <div class="layui-card" v-for="post in posts">
                        <div class="layui-card-body">
                            <div class="m-post">
                                <p>{{post.content}}</p>
                                <p class="m-post-footer">
                                    {{post.userName}} | {{post.postDate}}
                                </p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script src="/3rdparty/layui/layui.js"></script>
<script src="/js/jquery-3.4.1.js"></script>
<script src="/js/jquery.cookie.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="/js/status-check.js"></script>
<script>
    var app = new Vue({
        el: '#app',
        data: {
            followData: {
                following: '--',
                followers: '--'
            },
            username: $.cookie('userName'),
            userId: $.cookie('userId'),
            postContent: '',
            posts: []
        },
        mounted() {
            this.countFollowed();
            this.countFollowers();
            this.refreshPost();
        },
        methods: {
            async countFollowed() {
                let res = await axiosReq('get', "/users/" + $.cookie('userId') + "/followed/count");
                this.followData.following = res.data;
            },
            async countFollowers() {
                let res = await axiosReq('get', "/users/" + $.cookie('userId') + "/followers/count");
                this.followData.followers = res.data;
            },
            async addPost() {
                let res = await axiosReq('post', "/posts", {"content": this.postContent});
                if (res) {
                    this.refreshPost();
                }
            },
            async refreshPost(){
                let res = await axiosReq('get', "/posts");
                this.posts = res.data;
            },
            async logout(){
                await axiosReq('post', "/users/logout", {"token":$.cookie('token')});
                this.refreshPost();
            }
        }
    })
</script>


</html>